package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.CorpModel;
	import com.micmac.customer.view.RegionView;
	import com.micmac.customer.vo.RegionVO;
	
	import flash.events.Event;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;

	public class RegionViewMediator extends ModuleMediator
	{
		[Inject]
		public var regionView:RegionView;
		[Inject]
		public var customerService:ICustomerService;
		[Inject]
		public var _dataModel:CorpModel;
		private var _corpIndex:int = 0;
		override public function onRegister():void
		{
			addViewListener(RegionView.REGION_SAVE,onRegionSave);
			addViewListener(RegionView.REGION_PREV,onRegionPrevious);
			addViewListener(RegionView.REGION_ADD,onAddRegion);
			addViewListener(RegionView.REGION_NEXT,onRegionNext);
			addViewListener(RegionView.REGION_VIEW_CRAETED,onRegionViewCreated);
			eventMap.mapListener(eventDispatcher,CustomerEvent.REGION_SAVED,onRegionSaved);
		}
		
		private function onRegionSave(event:Event):void
		{
			var regionVO:RegionVO = new RegionVO();
			regionVO.regionName = regionView.regNameText.text;
			regionVO.corpName = regionView.regCombo.textInput.text;
			customerService.saveRegion(regionVO);
		}
		
		private function onRegionPrevious(event:Event):void
		{
			_corpIndex--;
			regionView.regNext.enabled = true;
			regionView.regSaveBtn.enabled = false;
			
			//regionView.regNameText.text = _dataModel.regionList[_corpIndex].regionName;
			//regionView.regCombo.textInput.text = _dataModel.regionList[_corpIndex].corpName;
			
			if(_corpIndex == 0)
			{
				regionView.prevBtn.enabled = false;
			}
		}
		
		private function onAddRegion(event:Event):void
		{
			regionView.clearHandler();
			regionView.regSaveBtn.enabled = true;
			
			regionView.regNext.enabled = false;
			//_corpIndex = _dataModel.regionList.length;
			//if(_dataModel.regionList.length != 1)
			//{
			//	regionView.prevBtn.enabled = true;
			//}
		}
		
		private function onRegionNext(event:Event):void
		{
			_corpIndex++;
			regionView.regSaveBtn.enabled = false;
			regionView.prevBtn.enabled = true;
			//regionView.regNameText.text = _dataModel.regionList[_corpIndex].regionName;
			//regionView.regCombo.textInput.text = _dataModel.regionList[_corpIndex].corpName;
			//if(_corpIndex == _dataModel.regionList.length - 1)
			//{
			//	regionView.regNext.enabled = false;
			//}
		}
		
		private function onRegionViewCreated(e:Event):void
		{
			regionView.regCombo.dataProvider = _dataModel.corpList;
		}
		
		private function onRegionSaved(event:CustomerEvent):void
		{
			//if(_dataModel.regionList.length != 1)
			//{
			//	regionView.prevBtn.enabled = true;
			//}
			
			regionView.addReg.enabled = true;
			regionView.regSaveBtn.enabled = false;
			//_corpIndex = _dataModel.regionList.length - 1;
		}
		
	}
}